<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        
    </style>
</head>
<body>
    
</body>
<script>

    document.onclick = function(){
        ajax({
            url:"https://wanandroid.com/wxarticle/chapters/json",
            success:res=>{
                console.log(res);
            }
        })
    }


    function ajax(ops){
        ops = ops || {};
        let {data={}, type="get", url, success, error} = ops;
        let str = "";
        for(let i in data){
            str += `${i}=${data[i]}&`;
        }
        if(type === "get"){
            url += "?" + str.slice(0,str.length-1);
        }
        const xhr = new XMLHttpRequest();
        xhr.open(type, url, true);
        xhr.onload = function(){
            if(xhr.status === 200){
                success(xhr.responseText);
            }else{
                error && error(xhr.status);
            }
        }
        if(type === "get"){
            xhr.send();
        }else{
            xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xhr.send(str);
        }
    }
</script>
</html>